/*
 * Written by Patrick Black , Dustin Striplin, SAMBA
 */

package Model;



import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.swing.JTable;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import FileParser.FileParser;

import Model.Roles;

import TableModels.AuthorTemplate;

public class Author extends User {
	
	public final static String TOO_MANY = "Maximum paper submissions already reached. Paper was not submitted.";
	public final static String DEADLINE = "Deadline for submission has been exceeded. Manuscript submission denied.";
	public final static String DUPLICATE = "Manuscript has already been submitted.";
	public final static String SUCCESS = "Manuscript submitted.";
	public final static String NOT_FOUND = "No such manscripts.";
	public final static String REMOVED = "Manuscript removed from conference.";
	public final static String NOT_SELECTED = "No manuscript selected";
	public final static String YES_DECISION = "can view reviews";
	public final static String NO_DECISION = "PC has not made a decision yet";
	
	private final static Integer MAX_PAPERS = 4;
	
	private String name;
	private Conference conference;	
	
	private FileParser parser;
	private ArrayList<Manuscript> manuscripts;
	
	
	/**
	 * Constructor
	 * @param a_fp File Parser
	 * @param a_name Author name
	 * @param a_conference Conference attending
	 * @param a_email
	 * @param a_phone
	 * @author Patrick Black
	 * @author Dustin Striplin
	 */
	public Author(FileParser a_fp, String a_name, Conference a_conference) {
		super(a_fp, a_name, a_conference);
		conference = a_conference;
		name = a_name;

		parser = a_fp;
		
		manuscripts = a_fp.getManuscripts(Roles.AUTHOR.toString(), a_name, a_conference.getName());
		

	}
	/**
	 * @author Dustin Striplin
	 * @param table the table to manipulate
	 * @return A string containing all of the text in the manuscript
	 */
	public String viewManuscript(final JTable table) {
		String manuscript = "";
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
							//must cast to correct TableModel, or getManuscriptLink wont work.

				
			String link = ((AuthorTemplate)table.getModel()).getManuscriptLink(row);//get link	
			InputStream stream = FileParser.class.getResourceAsStream(link);	//convert to stream
			try { // apache poi documentation  http://poi.apache.org/apidocs/index.html
					XWPFDocument docx = new XWPFDocument(stream);				//parse docx
					XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
					manuscript = we.getText();				
			
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
					stream.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		return manuscript;
	}
	
	
	/**
	 * 
	 * @return ArrayList of manuscripts for viewing
	 */
	public ArrayList<Manuscript> getManuscripts() {
		return manuscripts;
	}
	
	public FileParser getParser() {
		return parser;
	}
	 
	/**
	 * make changes to title or link of passed paper and save to XML file via parser.
	 * @author Patrick Black , SAMBA (SAVE MANUSCRIPT METHOD)
	 * @param a_manuscript paper to be modified
	 * @param a_title new title
	 * @param a_link new link
	 * @return the changed manuscript, null if error occurred
	 */
	public Manuscript modifyPaper(final Manuscript a_manuscript, final String a_title, final String a_link) {
		if(alreadyExists(a_manuscript.getTitle(), a_manuscript.getLink()) != true) {
			return null;
		}
		Manuscript m = new Manuscript(a_manuscript);
		m.setTitle(a_title);
		m.setLink(a_link);

		parser.saveManuscript(a_manuscript.getTitle(), m);
		manuscripts = parser.getManuscripts(Roles.AUTHOR.toString(), name, conference.getName());
		
		return m;
	}
	
	/**
	 * Submit a new manuscript to a conference
	 * @author Patrick Black, SAMBA (INSERT MANUSCRIPT METHOD)
	 * @param a_title title of new paper
	 * @param a_link link to new paper
	 * @return confirmation message
	 */
	public String submitPaper(final String a_title, final String a_link) {
		Manuscript new_manuscript = new Manuscript(name, a_title, a_link, conference);
		new_manuscript.setPc(conference.getPc());
		String confirmation = null;
		
		boolean check = alreadyExists(a_title, a_link);
		
		if(manuscripts.size() >= MAX_PAPERS) return TOO_MANY;
		
		if(getDate().before(conference.getDeadlineDate()) && check == false) {
			
			//submit paper
			
			ArrayList<Manuscript> mList = new ArrayList<Manuscript>();
			mList.add(new_manuscript);
			
			parser.insertManuscript(conference.getName(), mList);
			manuscripts = parser.getManuscripts(Roles.AUTHOR.toString(), name, conference.getName());
			confirmation = SUCCESS;
		} else if(check == true) {
			confirmation = DUPLICATE;
		} else {
			confirmation = DEADLINE;
		}
		
		return confirmation;
	}
	
	/**
	 * @return the last manuscript that was submitted
	 */
	public Manuscript getLatestManuscript() {
		return manuscripts.get(manuscripts.size()-1);
	}
	
	/**
	 * checks to see if a paper with the same title and link already exist in memory.
	 * @author Patrick Black
	 * @param a_title title of new paper
	 * @param a_link link to new paper
	 * @return true if exist false if not.
	 */
	private boolean alreadyExists(final String a_title, final String a_link) {
		boolean exists = false;
		for(int i = 0; i < manuscripts.size(); i++) {
			if (manuscripts.get(i).getTitle().equals(a_title) &&
					manuscripts.get(i).getLink().equals(a_link)) {
					exists = true;
				}
			}
		return exists;
	}
	

	
	/**
	 * Remove a manuscript from a conference
	 * @author Patrick Black , SAMBA (DELETE MANUSCRIPT METHOD)
	 * @param a_manuscript paper to be removed
	 * @return confirmation message
	 */
	public String unsubmitPaper(final Manuscript a_manuscript) {
		String confirmation = REMOVED;
		if(a_manuscript == null) {
			confirmation = NOT_SELECTED;
		}
		
		if(alreadyExists(a_manuscript.getTitle(), a_manuscript.getLink())) {
			ArrayList<Manuscript> list = new ArrayList<Manuscript>();
			list.add(a_manuscript);
			
			parser.deleteManuscript(Roles.AUTHOR.toString(), conference.getName(), list);
			manuscripts = parser.getManuscripts(Roles.AUTHOR.toString(), name, conference.getName());
		}
		return confirmation;
	}
	
	
	/**
	 * Get list of reviews for your viewing pleasure
	 * @author Patrick Black
	 * @param a_manuscript paper that was reviewed
	 * @return list of reviews, returns null if or pc hasn't made a decision yet
	 */
	public ArrayList<Review> getReviews(final Manuscript a_manuscript) {

		ArrayList<Review> list = a_manuscript.getReviews();
		return list;
	}
	
}